﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string un = txtUsername.Text;
            string pw = txtPassword.Text;
            try
            {
                if (txtUsername.Text == "" || txtPassword.Text == "")
                {
                    label1.Text = "You must enter all information. Please try again";
                    label1.ForeColor = Color.Red;
                }
                else
                {

                    if (Common.checklogin(un, pw) != null)
                    {
                        
                        frmMain main = new frmMain();
                        if (Common.checklogin(un, pw) == "Admin")
                        {
                            label1.Text = "";
                            Common.username.Add(un);
                            Common.password.Add(pw);
                            Common.permission = Common.checklogin(un, pw);
                            this.Hide();
                            main.Show();
                        }
                        else if (Common.checklogin(un, pw) == "PM/PL")
                        {
                            label1.Text = "";
                            Common.username.Add(un);
                            Common.password.Add(pw);
                            Common.permission = Common.checklogin(un, pw);
                            this.Hide();
                            main.Show();
                        }
                        else if (Common.checklogin(un, pw) == "Staff")
                        {
                            label1.Text = "";
                            Common.username.Add(un);
                            Common.password.Add(pw);
                            Common.permission = Common.checklogin(un, pw);
                            this.Hide();
                            main.Show();
                        }
                        else if (Common.checklogin(un, pw) == "")
                        {
                            label1.Text = "User name or password incorrect. Please try again!"; label1.ForeColor = Color.Red;
                            txtUsername.Clear();
                            txtPassword.Clear();
                            txtUsername.Focus();
                        }
                    }
                }
            }
            catch
            {
                label1.Text = "Account not exited"; label1.ForeColor = Color.Red; txtUsername.Clear();
                txtPassword.Clear();
                txtUsername.Focus();
            }
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}
